package com.example.graduatetest.model;

import android.text.TextUtils;
import android.util.Log;

import com.example.graduatetest.bean.Card;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.bean.Score;
import com.example.graduatetest.callback.CardCallBack;
import com.example.graduatetest.callback.CardListCallBack;
import com.example.graduatetest.callback.NoticeListCallBack;
import com.example.graduatetest.callback.OneClassCallBack;
import com.example.graduatetest.callback.ScoreListCallBack;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.utils.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class NoticeModel {
    private static String www = Constant.www;
    private static String insertOneNoticeUrl = "/notice/insertOneNotice";
    private static String selectNoticeListByUserUrl = "/notice/selectNoticeListByUser";
    private static String selectNoticeListByUserAndLabelUrl = "/notice/selectNoticeListByUserAndLabel";
    private static String selectNoticeListByUserAndLeaveUrl = "/notice/selectNoticeListByUserAndLeave";
    private static String updateNoticeStatusUrl = "/notice/updateNoticeStatus";
    //插入成绩
    public static void setInsertOneNotice(final String title,
                                      final String content,
                                      final String send_name,
                                      final String send_number,
                                      final String receive_number,
                                      final String label,
                                      final String class_number,
                                      final String class_code,
                                      final String status,
                                      final StringCallBack callBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("title", title)
                            .add("content",content)
                            .add("send_name", send_name)
                            .add("send_number", send_number)
                            .add("receive_number", receive_number)
                            .add("label",label)
                            .add("class_number",class_number)
                            .add("class_code",class_code)
                            .add("status",status)
                            .build();

                    Request request = new Request.Builder()
                            .url(www+insertOneNoticeUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //插入成绩
    public static void updateNoticeStatus(final String id,final StringCallBack callBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("id", id)
                            .build();

                    Request request = new Request.Builder()
                            .url(www+updateNoticeStatusUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }
    public static void selectNoticeListByUserAndType(final String number,
                                                     final String type,
                                                     final String class_number,
                                                     final NoticeListCallBack callBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectNoticeListByUserAndLabelUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("number", number);
                urlBuilder.addQueryParameter("label",type);
                urlBuilder.addQueryParameter("class_number",class_number);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONNoticeList(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }
    public static void selectNoticeListByUserAndType(final String number,
                                                     final String type,
                                                     final NoticeListCallBack callBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectNoticeListByUserAndLeaveUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("number", number);
                urlBuilder.addQueryParameter("label",type);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONNoticeList(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public static void selectNoticeListByUser(final String number,
                                              final NoticeListCallBack callBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectNoticeListByUserUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("number", number);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONNoticeList(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //解析json对象
    private static void parseJSONNoticeList(String jsonData, NoticeListCallBack callBack){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Score List对象
            Log.e("haha", jsonArray.toString());
            List<Notice> noticeList = gson.fromJson(jsonArray.toString(), new TypeToken<List<Notice>>(){}.getType());
            callBack.onSuccess(noticeList);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }
}
